﻿using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace Omu.AwesomeDemo.Tests.UI
{
    [TestFixture]
    public class LookupTests
    {
        private IWebDriver d;

        [SetUp]
        public void SetUp()
        {
            d = new FirefoxDriver();
            d.Navigate().GoToUrl("http://o/awesome/lookupdemo");
        }

        [Test]
        public void LookupDoubleClickSelect()
        {
            d.FindElement(By.Id("lpoCountryId")).Click();
            d.FindElement(By.XPath("//ul[@id='CountryIdls']/li")).Click();
            var val = d.FindElement(By.XPath("//ul[@id='CountryIdls']/li")).GetAttribute("data-value");
            d.FindElement(By.XPath("//ul[@id='CountryIdls']/li")).Click();

            Assert.AreEqual(val, d.FindElement(By.Name("CountryId")).GetAttribute("value"));
        }

        [Test]
        public void SelectByRowClickAndOkButton()
        {
            d.FindElement(By.Id("lpoCountryId")).Click();
            d.FindElement(By.XPath("//ul[@id='CountryIdls']/li")).Click();
            var val = d.FindElement(By.XPath("//ul[@id='CountryIdls']/li")).GetAttribute("data-value");
            d.FindElement(By.XPath("//button[@type='button']")).Click();

            Assert.AreEqual(val, d.FindElement(By.Name("CountryId")).GetAttribute("value"));
        }

        [Test]
        public void ClearButtonClickClearsTheValue()
        {
            d.FindElement(By.Id("lcCountryId")).Click();

            Assert.AreEqual("", d.FindElement(By.Id("ldCountryId")).GetAttribute("value"));
            Assert.AreEqual("", d.FindElement(By.Id("CountryId")).GetAttribute("value"));
        }

        [Test]
        public void ClickMoreButtonToGetMoreResults()
        {
            d.FindElement(By.Id("lpoPerson")).Click();
            d.FindElement(By.XPath("//ul[@id='Personls']/a")).Click();
            var val = d.FindElement(By.XPath("//ul[@id='Personls']/li[13]")).GetAttribute("data-value");
            d.FindElement(By.XPath("//ul[@id='Personls']/li[13]")).Click();

            d.FindElement(By.XPath("//div[5]/div[3]/div/button")).Click();

            Assert.AreEqual(val, d.FindElement(By.Id("Person")).GetAttribute("value"));
        }

        [Test]
        public void SelectMultiple()
        {
            d.FindElement(By.Id("lcHobbies")).Click();

            Assert.IsEmpty(d.FindElement(By.Id("ldHobbies")).Text);

            d.FindElement(By.Id("lpoHobbies")).Click();
            d.FindElement(By.LinkText("+")).Click();
            d.FindElement(By.LinkText("+")).Click();
            d.FindElement(By.XPath("//div[8]/div[3]/div/button")).Click();

            Assert.IsNotEmpty(d.FindElement(By.Id("ldHobbies")).Text);
        }
    }
}
